﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.SixAcquaint.BLL;
using Yipusoft.SixAcquaint.Model;
using System.Collections.Generic;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;

public partial class BasicBase_KeyUnit_UpdateKeyUnit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
            DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
            if (userInfo.DepartmentID.Substring(0, 4) == "1300")
            {
                DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') and DepartmentID<>'1300000' ");
                DepartmentList.DataBind();
                DepartmentListBind();
            }
            else
            {
                DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" DepartmentID= '" + department.DepartmentID.Substring(0, 4) + "000' ");
                DepartmentList.DataBind();
                DepartmentListBind();
            }
            BindStandards();
            //BindDepartment("201200", "201201", "201202"); ;
            if (!string.IsNullOrEmpty(Request["ID"]))
            {
                KeyUnitInfo keyUnitDtails = KeyUnit.GetKeyUnitInfoByID(Request["ID"]);
                txtAddress.Text = keyUnitDtails.Address;

                DepartmentList.SelectedValue = keyUnitDtails.DepartmentID.Substring(0, 4) + "000";
                UnitList.SelectedValue = keyUnitDtails.DepartmentID;
                txtEast.Text = keyUnitDtails.East;
                //keyUnitDtails.StandardID = Request["sid"];
                txtPeople.Text = keyUnitDtails.Employee.ToString();
                txtCorpTel.Text = keyUnitDtails.FaRenTel;
                txtCorporation.Text = keyUnitDtails.FaRenDaiBiao;
                txtPricTel.Text = keyUnitDtails.FuZeRenTel;
                txtAsserts.Text = keyUnitDtails.GuDingZiChan.ToString();
                txtJingDu.Text = keyUnitDtails.JingDu.ToString();
                txtWeiDu.Text = keyUnitDtails.WeiDu.ToString();
                txtJZhArea.Text = keyUnitDtails.JZhArea.ToString();
                txtIncome.Text = keyUnitDtails.NianZongChanZhi.ToString();
                txtNorth.Text = keyUnitDtails.North;
                txtSouth.Text = keyUnitDtails.South;
                ddlPossessive.SelectedValue = keyUnitDtails.SuoYouZhi;
                txtUnitName.Text = keyUnitDtails.UnitName;
                txtWest.Text = keyUnitDtails.West;
                txtPrincipal.Text = keyUnitDtails.XiaoFangFuZeRen;
                txtZDArea.Text = keyUnitDtails.ZhDArea.ToString();
                ddlStandards.SelectedValue = keyUnitDtails.StandardID;
            }
        }
    }

    void DepartmentListBind()
    {
        string strWhere = "departmentid like '" + DepartmentList.SelectedValue.Substring(0, 4) + "3%' or departmentid like '" + DepartmentList.SelectedValue.Substring(0, 4) + "1%' ";
        UnitList.DataSource = Department.GetList(strWhere);
        UnitList.DataBind();
    }

    private void BindDepartment(string permissionAdmin, string permissionLevel, string permissionUnit)
    {
        if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionAdmin))
        {
            foreach (DepartmentInfo department in Department.GetList("RIGHT(DepartmentID, 2) = '00'"))
            {
                DepartmentList.Items.Add(new ListItem(department.FullName, department.DepartmentID));
            }
        }
        else
        {
            DepartmentList.DataSource = GetDepartmentList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID, permissionLevel, permissionUnit);
            DepartmentList.DataTextField = "FullName";
            DepartmentList.DataValueField = "DepartmentID";
            DepartmentList.DataBind();

        }
    }
    private List<DepartmentInfo> GetDepartmentList(string departmentID, string permissionLevel, string permissionUnit)
    {
        List<DepartmentInfo> departmentList = new List<DepartmentInfo>();
        if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionLevel) || Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionUnit))
            departmentList = Department.GetList("DepartmentID='" + departmentID.Substring(0, 4) + "000'");
        return departmentList;
    }

    private void BindStandards()
    {
        ddlStandards.DataSource = KeyUnit.GetSonStandard();
        ddlStandards.DataTextField = "Standard";
        ddlStandards.DataValueField = "StandardID";
        ddlStandards.DataBind();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        KeyUnitInfo keyUnitDtails = KeyUnit.GetKeyUnitInfoByID(Request["ID"]);
        keyUnitDtails.Address = txtAddress.Text;
        keyUnitDtails.DepartmentID = UnitList.SelectedValue;
        keyUnitDtails.East = txtEast.Text;
        keyUnitDtails.StandardID = ddlStandards.SelectedValue;
        keyUnitDtails.Employee = int.Parse(txtPeople.Text == "" ? "0" : txtPeople.Text);
        keyUnitDtails.FaRenTel = txtCorpTel.Text;
        keyUnitDtails.FaRenDaiBiao = txtCorporation.Text;
        keyUnitDtails.FuZeRenTel = txtPricTel.Text;
        keyUnitDtails.GuDingZiChan = Convert.ToDecimal(txtAsserts.Text == "" ? "0" : txtAsserts.Text);
        keyUnitDtails.JingDu = Convert.ToDecimal(txtJingDu.Text == "" ? "0" : txtJingDu.Text);
        keyUnitDtails.WeiDu = Convert.ToDecimal(txtWeiDu.Text == "" ? "0" : txtWeiDu.Text);
        keyUnitDtails.JZhArea = Convert.ToDecimal(txtJZhArea.Text == "" ? "0" : txtJZhArea.Text);
        keyUnitDtails.NianZongChanZhi = Convert.ToDecimal(txtIncome.Text == "" ? "0" : txtIncome.Text);
        keyUnitDtails.North = txtNorth.Text;
        keyUnitDtails.South = txtSouth.Text;
        keyUnitDtails.SuoYouZhi = ddlPossessive.SelectedValue;
        keyUnitDtails.UnitName = txtUnitName.Text;
        keyUnitDtails.West = txtWest.Text;
        keyUnitDtails.XiaoFangFuZeRen = txtPrincipal.Text;
        keyUnitDtails.ZhDArea = Convert.ToDecimal(txtZDArea.Text == "" ? "0" : txtZDArea.Text);


        if (KeyUnit.UpdateKeyUnit(keyUnitDtails))
        {
            Response.Redirect("InsertKeyUnit.aspx?sid=" + ddlStandards.SelectedValue);
        }
    }

    protected void DepartmentList_SelectedIndexChanged1(object sender, EventArgs e)
    {
        DepartmentListBind();
    }
}
